{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 独立使用django model（一）\n",
    "# Django Shell\n",
    "# 项目根目录中打开Django Shell : python manage.py shell\n",
    "# import django\n",
    "# django.setup()  # 装载Django\n",
    "\n",
    "# 独立使用django model（二）\n",
    "# python ×××.py or Jupyter Notebook\n",
    "# 将脚本放置在项目根目录下jupyter_notebook文件中比较方便\n",
    "import sys\n",
    "import os\n",
    "import json\n",
    "import requests\n",
    "from django.core.files import File\n",
    "from django.core.files.base import ContentFile\n",
    "# 外部脚本链接django项目\n",
    "# 添加环境变量\n",
    "'''\n",
    "print(os.path.abspath('__file__'))\n",
    "print(os.path.dirname(os.path.abspath('__file__')))\n",
    "print(os.path.dirname(os.path.dirname(os.path.abspath('__file__'))))\n",
    "/Users/zhaojinhui/Desktop/webapp/backend/rmis/jupyter_notebook/__file__\n",
    "/Users/zhaojinhui/Desktop/webapp/backend/rmis/jupyter_notebook\n",
    "/Users/zhaojinhui/Desktop/webapp/backend/rmis\n",
    "'''\n",
    "project = os.path.dirname(os.getcwd())  # get current work directory\n",
    "sys.path.append(project)\n",
    "sys.path.append(os.path.join(project, 'rmis'))\n",
    "# sys.path.append已设置临时环境变量，可以直接调用其中的文件，脚本是外部脚本，只是放在了项目当中而已\n",
    "os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')\n",
    "# 相关数据库配置等都在settings.py文件中\n",
    "# 导入并装载django\n",
    "import django\n",
    "django.setup()\n",
    "# 脚本正文"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import date, datetime, timedelta\n",
    "import time\n",
    "import random \n",
    "import pandas as pd\n",
    "from user.models import Clerk, Customer\n",
    "from organization.models import Store \n",
    "\n",
    "\n",
    "sale_num = [1,3,5]  # 一天销售单数量\n",
    "\n",
    "# 必要字段 fk_customer fk_store fk_clerk created\n",
    "store_id_list = [ store.id for store in Store.objects.filter(type='retail') ]\n",
    "customer_id_list = [ customer.id for customer in Customer.objects.all() ]\n",
    "clerk_id_list = [ clerk.id for clerk in Clerk.objects.all() ]\n",
    "\n",
    "from_date = date(2017,1,1)  # 引用值\n",
    "to_date =  date(2019,5,18)\n",
    "sales = []\n",
    "while from_date <= to_date:\n",
    "    for i in range(0, random.choice(sale_num)):  # 随机订单数量\n",
    "        sale_datetime = datetime(\n",
    "            from_date.year,\n",
    "            from_date.month,\n",
    "            from_date.day,\n",
    "            random.randint(10,20),  # randint 闭区间\n",
    "            random.randint(0,59),\n",
    "            random.randint(0,59)\n",
    "        )\n",
    "        sale = {\n",
    "            'fk_customer' : random.choice(customer_id_list),\n",
    "            'fk_store' : random.choice(store_id_list),\n",
    "            'fk_clerk' : random.choice(clerk_id_list),\n",
    "            'created' : sale_datetime\n",
    "        }\n",
    "        sales.append(sale)\n",
    "        \n",
    "    from_date += timedelta(days=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试生成excel文件\n",
    "df = pd.DataFrame.from_dict(sales)\n",
    "df.to_excel(\"./excel/invoice.sale.20170101.20190518.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0, {})"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# from invoice.models import Sale,GoodInSale, Purchase, GoodInPurchase\n",
    "# Sale.objects.all().delete()\n",
    "# GoodInSale.objects.all().delete()\n",
    "# Purchase.objects.all().delete()\n",
    "# GoodInPurchase.objects.all().delete()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
